package com.yunbiao.ybbusstopplate.refactoring.ui.dalian;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.widget.ArrayAdapter;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.example.library_hy_advert.system.Cache;
import com.github.gzuliyujiang.wheelview.contract.OnWheelChangedListener;
import com.github.gzuliyujiang.wheelview.widget.WheelView;
import com.yunbiao.ybbusstopplate.R;
import com.yunbiao.ybbusstopplate.base.BaseSettingFragment;
import com.yunbiao.ybbusstopplate.databinding.FragmentDalianSettingBinding;
import com.yunbiao.ybbusstopplate.refactoring.Config;

import java.io.File;
import java.util.List;

public class DalianSettingFragment extends BaseSettingFragment {
    private static final String TAG = "DalianShanghaiSettingFr";

    private FragmentDalianSettingBinding binding;

    @Override
    public int getViewId() {
        return R.layout.fragment_dalian_setting;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.bind(requireView());

        List<Config.DeviceType> deviceTypes = Config.DeviceType.list();
        ArrayAdapter<Config.DeviceType> devicesAdapter = new ArrayAdapter<>(requireContext(),R.layout.myspiner,deviceTypes);
        devicesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        binding.spDeviceType.setAdapter(devicesAdapter);
        Config.DeviceType deviceType = Config.getDeviceType();
        int deviceTypeIndex = deviceTypes.indexOf(deviceType);
        binding.spDeviceType.setSelection(deviceTypeIndex);

        binding.tvSaveDeviceType.setOnClickListener(v -> save());

        List<Config.Agreement> list = Config.Agreement.list();
        ArrayAdapter<Config.Agreement> adapter = new ArrayAdapter<>(requireContext(),R.layout.myspiner,list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        binding.spAgreement.setAdapter(adapter);
        Config.Agreement agreement = Config.getAgreement();
        int index = list.indexOf(agreement);
        binding.spAgreement.setSelection(index);

        binding.tvSaveAgreement.setOnClickListener(v -> save());

        String phoneNumber = Cache.getString(Config.Key.PHONE_NUMBER.name(),Config.Default.PHONE_NUMBER);
        binding.edtPhoneNumber.setText(phoneNumber);
        binding.btnPhoneNumber1.setOnClickListener(v -> {
            binding.edtPhoneNumber.setText("968600");
        });
        binding.btnPhoneNumber2.setOnClickListener(v -> {
            binding.edtPhoneNumber.setText("85624689");
        });

        binding.tvSavePhoneNumber.setOnClickListener(v -> save());

        binding.btnSetDefaultHeadInfoFixedPromotionSlogan.setOnClickListener(v -> {
            binding.edtHeadInfoFixedPromotionSlogan.setText(binding.btnSetDefaultHeadInfoFixedPromotionSlogan.getText().toString());
        });

        binding.edtHeadInfoFixedPromotionSlogan.setText(Cache.getString(ZHIDAParser.KEY_FIXED_PROMOTION_SLOGAN));
        binding.tvSaveHeadInfoFixedPromotionSlogan.setOnClickListener(V -> save());
    }

    @Override
    protected void initData() {

    }

    private void save(){
        boolean isChanged = false;
        Config.DeviceType selectDeviceType = (Config.DeviceType) binding.spDeviceType.getSelectedItem();
        Config.DeviceType currDeviceType = Config.getDeviceType();
        if(selectDeviceType.ordinal() != currDeviceType.ordinal()){
            Config.setDeviceType(selectDeviceType);
            isChanged = true;
        }

        Config.Agreement selectAgreement = (Config.Agreement) binding.spAgreement.getSelectedItem();
        Config.Agreement currAgreement = Config.getAgreement();
        if(selectAgreement.ordinal() != currAgreement.ordinal()){
            Config.setAgreement(selectAgreement);
            isChanged = true;
        }

        String phoneNumber = binding.edtPhoneNumber.getText().toString();
        String mCachePhoneNumber = Cache.getString(Config.Key.PHONE_NUMBER.name());
        if(!TextUtils.equals(mCachePhoneNumber,phoneNumber)){
            Cache.setString(Config.Key.PHONE_NUMBER.name(), phoneNumber);
            isChanged = true;
        }

        String headInfoFixedPromotionSlogan = binding.edtHeadInfoFixedPromotionSlogan.getText().toString();
        String mCacheHeadInfoFixedPromotionSlogan = Cache.getString(ZHIDAParser.KEY_FIXED_PROMOTION_SLOGAN);
        if(!TextUtils.equals(mCacheHeadInfoFixedPromotionSlogan,headInfoFixedPromotionSlogan)){
            Cache.setString(ZHIDAParser.KEY_FIXED_PROMOTION_SLOGAN,headInfoFixedPromotionSlogan);
            isChanged = true;
        }

        ToastUtils.getDefaultMaker().setGravity(Gravity.CENTER,0,0).setDurationIsLong(false).show("已保存");

        if(isChanged){
            ProgressDialog progressDialog = new ProgressDialog(requireContext());
            progressDialog.setMessage("正在重启APP...");
            progressDialog.show();

            requireView().postDelayed(() -> {
                AppUtils.relaunchApp(true);
            },1500);
        }
    }
}
